package cn.sjxy.apiClient;

import cn.sjxy.baseLib.AjaxResult;
import cn.sjxy.baseLib.PageInfo;
import cn.sjxy.config.mvcExtend.NeedUserLogin;
import cn.sjxy.model.Md_LoginUser;
import cn.sjxy.model.Md_Shopping;
import cn.sjxy.service.Sv_Shopping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 客户端 - 购物车API
 */
@RestController
@Validated
@Api(tags = "客户端-购物车")
public class Ci_Shopping {

    @Autowired
    private Sv_Shopping sv_shopping;

    /**
     * 添加商品到购物车
     * @param request
     * @param bookCode
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/shopping/addShopping")
    @ApiOperation("添加商品到购物车")
    @NeedUserLogin
    public AjaxResult addShopping(HttpServletRequest request,
                                  @NotBlank(message = "图书编号不能为空") String bookCode) throws Exception {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");
        sv_shopping.addShopping(loginUser.getUsername(), bookCode);
        return AjaxResult.success("添加成功");
    }

    /**
     * 获取购物车列表（分页）
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/shopping/getTable")
    @ApiOperation("获取购物车列表")
    @NeedUserLogin
    public AjaxResult getTable(HttpServletRequest request,
                               @RequestParam(defaultValue = "1") Integer pageIndex,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");
        List<Md_Shopping> shoppingList = sv_shopping.getShoppingList(loginUser.getUsername());

        // 手动分页
        int start = (pageIndex - 1) * pageSize;
        int end = Math.min(start + pageSize, shoppingList.size());
        List<Md_Shopping> pageList = shoppingList.subList(start, end);

        // 构造分页信息
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRowCount(Long.valueOf(shoppingList.size()));
        // 计算总页数
        pageInfo.setPageCount((long) Math.ceil((double) shoppingList.size() / pageSize));

        AjaxResult result = AjaxResult.success("查询成功");
        result.addTable(pageList);
        result.addKey("pager", pageInfo);  // 直接使用 addKey 添加 PageInfo
        return result;
    }

    /**
     * 增加购物车商品数量
     * @param request
     * @param bookCode
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/shopping/addQty")
    @ApiOperation("增加购物车商品数量")
    @NeedUserLogin
    public AjaxResult addQty(HttpServletRequest request,
                             @NotBlank(message = "图书编号不能为空") String bookCode) throws Exception {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");
        sv_shopping.addQty(loginUser.getUsername(), bookCode);
        return AjaxResult.success("操作成功");
    }

    /**
     * 减少购物车商品数量
     * @param request
     * @param bookCode
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/shopping/subQty")
    @ApiOperation("减少购物车商品数量")
    @NeedUserLogin
    public AjaxResult subQty(HttpServletRequest request,
                             @NotBlank(message = "图书编号不能为空") String bookCode) throws Exception {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");
        sv_shopping.subQty(loginUser.getUsername(), bookCode);
        return AjaxResult.success("操作成功");
    }

    /**
     * 删除购物车商品
     * @param request
     * @param bookCode
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/shopping/delete")
    @ApiOperation("删除购物车商品")
    @NeedUserLogin
    public AjaxResult delete(HttpServletRequest request,
                             @NotBlank(message = "图书编号不能为空") String bookCode) throws Exception {
        Md_LoginUser loginUser = (Md_LoginUser) request.getAttribute("loginUser");
        sv_shopping.deleteShopping(loginUser.getUsername(), bookCode);
        return AjaxResult.success("删除成功");
    }
}

